﻿/*-----------------------------------/
/* Create By.Ben 20181206
/*----------------------------------*/
var loginFormVue = new Vue({
    el: '#loginForm',
    data: {
        username: "",
        password: "",
        isLdapLogin: false,
        isRememberMe: false,
        loginBtn: "",
        isDisabled: false
    },
    methods: {
        userLogin: function () {
            var vue = this;
            setLoadingButton(vue);

            //登录请求
            $.ajax({
                type: "POST",
                url: "/Account/LoginUser",
                data: {
                    username: vue.username,
                    password: vue.password,
                    isLdapLogin: vue.isLdapLogin
                },
                success: function (res) {
                    if (res.status == "Success") {
                        //记住我
                        if (vue.isRememberMe)
                            setRememberMe(vue);
                        else
                            clearRememberMe(vue);
                        //初始Global中用户及数据字典的数据
                        GlobalUser.setUserInfo(res.data);
                        GlobalDictionary.SetGlobalDictionary();
                        //跳转页面
                        var returnUrl = GetQueryString("ReturnUrl");
                        if (returnUrl != null) {
                            window.location.href = returnUrl;
                        } else {
                            window.location.href = "/Home/Index";
                        }
                    } else {
                        toastr.error(res.message);
                        setLoginButton(vue);
                    }
                },
                error: function (err) {
                    console.log(err);
                    setLoginButton(vue);
                }
            });
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        setLoginButton(this);
        initRememberMe(this);
    }
});

function setLoginButton(vue) {
    vue.loginBtn = "登入Login";
    vue.isDisabled = false;
}

function setLoadingButton(vue) {
    vue.loginBtn = "请稍候please wait a moment...";
    vue.isDisabled = true;
}

function initRememberMe(vue) {
    var info = $.cookie("AZ_EQ_USER");
    if (info != undefined) {
        var user = info.split("@");
        if (user.length > 2) {
            vue.username = user[0];
            vue.password = user[1];
            vue.isLdapLogin = user[2];
            vue.isRememberMe = true;
            $("#password").val(vue.password);
        }
    }
}

function setRememberMe(vue) {
    var info = vue.username + "@" + vue.password + "@" + vue.isLdapLogin;
    $.cookie("AZ_EQ_USER", info, { expires: 7 });
}

function clearRememberMe(vue) {
    $.removeCookie('AZ_EQ_USER');
}